package com.example.victory.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期辅助类
 */
public class DateHelper {
    public static final String DATE_FORMAT_TIME = "HH:mm:ss";
    public static final String DATE_FORMAT_DATE = "yyyy-MM-dd";
    public static final String DATE_FORMAT_MONTH = "yyyy-MM";
    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
    public static final String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_DATETIME_MM = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_DATETIME_MS = "yyyy-MM-dd HH:mm:ss.SSS";

    public static Date parseTime(String str) throws ParseException {
        if (str == null)
            return null;
        else {
            SimpleDateFormat sdfTime = new SimpleDateFormat(DATE_FORMAT_TIME);
            sdfTime.setLenient(false);
            return sdfTime.parse(str);
        }
    }

    public static Date parseDate(String str) throws ParseException {
        if (str == null)
            return null;
        else {
            SimpleDateFormat sdfDate = new SimpleDateFormat(DATE_FORMAT_DATE);
            sdfDate.setLenient(false);
            return sdfDate.parse(str);
        }
    }

    public static Date parseDateYYYYMMDD(String str) throws ParseException {
        if (str == null)
            return null;
        else {
            SimpleDateFormat sdfDate = new SimpleDateFormat(DATE_FORMAT_YYYYMMDD);
            sdfDate.setLenient(false);
            return sdfDate.parse(str);
        }
    }

    public static Date parseDateTimeMM(String str) throws ParseException {
        if (str == null)
            return null;
        else {
            SimpleDateFormat sdfDate = new SimpleDateFormat(DATE_FORMAT_DATETIME_MM);
            sdfDate.setLenient(false);
            return sdfDate.parse(str);
        }
    }

    public static Date parseDateTime(String str) throws ParseException {
        if (str == null)
            return null;
        else {
            SimpleDateFormat sdfDateTime = new SimpleDateFormat(DATE_FORMAT_DATETIME);
            sdfDateTime.setLenient(false);
            return sdfDateTime.parse(str);
        }
    }

    public static Date parseDateTimeMs(String str) throws ParseException {
        if (str == null)
            return null;
        else {
            SimpleDateFormat sdfDateTimeMs = new SimpleDateFormat(DATE_FORMAT_DATETIME_MS);
            sdfDateTimeMs.setLenient(false);
            return sdfDateTimeMs.parse(str);
        }
    }

    public static Date parseMonth(String str) throws ParseException {
        if (str == null)
            return null;
        else {
            SimpleDateFormat sdfDateTimeMs = new SimpleDateFormat(DATE_FORMAT_MONTH);
            sdfDateTimeMs.setLenient(false);
            return sdfDateTimeMs.parse(str);
        }
    }

    public static String getTime(Date date) {
        if (date == null)
            return null;
        else {
            SimpleDateFormat sdfTime = new SimpleDateFormat(DATE_FORMAT_TIME);
            return sdfTime.format(date);
        }
    }

    public static String getDate(Date date) {
        if (date == null)
            return null;
        else {
            SimpleDateFormat sdfDate = new SimpleDateFormat(DATE_FORMAT_DATE);
            return sdfDate.format(date);
        }
    }

    public static String getDateYYYYMMDD(Date date) {
        if (date == null)
            return null;
        else {
            SimpleDateFormat sdfDate = new SimpleDateFormat(DATE_FORMAT_YYYYMMDD);
            return sdfDate.format(date);
        }
    }

    public static String getDateTime(Date date) {
        if (date == null)
            return null;
        else {
            SimpleDateFormat sdfDateTime = new SimpleDateFormat(DATE_FORMAT_DATETIME);
            return sdfDateTime.format(date);
        }
    }

    public static String getDateTimeMM(Date date) {
        if (date == null)
            return null;
        else {
            SimpleDateFormat sdfDateTime = new SimpleDateFormat(DATE_FORMAT_DATETIME_MM);
            return sdfDateTime.format(date);
        }
    }

    public static String getDateTimeMs(Date date) {
        if (date == null)
            return null;
        else {
            SimpleDateFormat sdfDateTimeMs = new SimpleDateFormat(DATE_FORMAT_DATETIME_MS);
            return sdfDateTimeMs.format(date);
        }
    }

    public static String getMonth(Date date) {
        if (date == null)
            return null;
        else {
            SimpleDateFormat sdfMonth = new SimpleDateFormat(DATE_FORMAT_MONTH);
            return sdfMonth.format(date);
        }
    }

    public static Date getCurrentDate() {
        return new Date();
    }

    /**
     * 判断字符串是否是日期格式
     *
     * @param dateValue  日期字符串
     * @param dateFormat 日期格式
     * @return
     */
    public static boolean isDateString(String dateValue, String dateFormat) {
        try {
            SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
            Date dd = fmt.parse(dateValue);
            if (dateValue.equals(fmt.format(dd))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 将日期转换为Double类型（值为yyyyMMdd.HHmmss）
     *
     * @param date 日期
     * @return
     */
    public static double dateToDouble(Date date) {
        if (date == null)
            return 0;
        String dateStr = getDateTime(date).replace("-", "").replace(" ", ".").replace(":", "");
        return Double.parseDouble(dateStr);
    }

    /**
     * 对日期进行加减
     *
     * @param date  待处理的日期
     * @param value 加减值
     * @param unit  加减单位，可以为Calendar.YEAR=1,Calendar.MONTH=2,Calendar.DATE=5,Calendar.HOUR=10,Calendar.MINUTE=12,Calendar.SECOND=13
     * @return 返回计算后的新日志
     */
    public static Date increment(Date date, int value, int unit) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(unit, value);
        return cal.getTime();
    }
}
